package org.ws.edu.task3.ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

public class MenuDispatcher {
    private ConsoleHandlerUI mConsoleHandler = null;
    private static Logger logger = Logger.getLogger(MenuDispatcher.class);

    public MenuDispatcher(ConsoleHandlerUI handler) {
        mConsoleHandler = handler;
    }

    public void run() {

        while (true) {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            try {
                String[] menu = { "Available Tests", "Invalid Tests", "Help", "Exit", };
                for (int i = 0; i < menu.length; i++) {
                    System.out.println(i + 1 + ". " + menu[i]);
                }
                System.out.println("Enter the number of operation...");
                int numberOfOperation = Integer.parseInt(in.readLine()) - 1;
                switch (numberOfOperation) {
                    case 0:
                        mConsoleHandler.chooseTestName();
                        mConsoleHandler.handle();
                        break;
                    case 1:
                        mConsoleHandler.typeInvalidTestNames();
                        System.out.println();
                        break;
                    case 2:
                        System.out.println("Help");
                        System.out.println("If Test is MultiAnswer type you must respond on question in format \"2, 4\"\n");
                        System.out.println("If Test is Scales type you must respond on question with one answer\n");
                        logger.info("Switch \"Help\" element of menu");
                        break;
                    case 3:
                        System.exit(0);
                        logger.info("Close program");
                        break;
                }
            } catch (IOException e) {
                logger.debug("IOException", e);
            } catch (NumberFormatException e) {
                logger.info("Incorrect test number", e);
                continue;
            }
        }
    }
}
